//
//  FANVisitorView.m
//  Weibo - OC
//
//  Created by user on 16/4/17.
//  Copyright © 2016年 user. All rights reserved.
//

#import "FANVisitorView.h"

//#warning 宏定义一定要放到 导入头文件的 上面, 会影响编译

//define this constant if you want to use Masonry without the 'mas_' prefix

#define MAS_SHORTHAND

//define this constant if you want to enable auto-boxing for default syntax

#define MAS_SHORTHAND_GLOBALS

#import <Masonry/Masonry.h>

@interface FANVisitorView ()

@property (nonatomic, strong) UIImageView *iconImageView;

@property (nonatomic, strong) UIImageView *circleImageView;

@property (nonatomic, strong) UILabel *midlabel;

@property (nonatomic, strong) UIButton *registerbutton;

@property (nonatomic, strong) UIButton *loginButton;

@property (nonatomic, strong) UIImageView *coverImageView;

@end

@implementation FANVisitorView

- (UIImageView *)coverImageView{
    
    if (!_coverImageView) {
        
        _coverImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"]];
        
        [_coverImageView sizeToFit];
        
    }
    return _coverImageView;
}

- (UIButton *)loginButton{
    
    if (!_loginButton) {
        
        _loginButton = [[UIButton alloc] init];
        
        [_loginButton  setBackgroundImage:[UIImage imageNamed:@"common_button_white"] forState:UIControlStateNormal];
        
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
        
        [_loginButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        
        [_loginButton addTarget:self action:@selector(didClickLogin) forControlEvents:UIControlEventTouchUpInside];
        
        _loginButton.titleLabel.font = [UIFont systemFontOfSize:15];
        
    }
    return _loginButton;
}

- (void)didClickLogin{
    
    if ([self.delegate respondsToSelector:@selector(didClickLogin)]) {
        
        [self.delegate didClickLogin];
    }
    
}

- (void)didClickRegister{
    
    if ([self.delegate respondsToSelector:@selector(didClickRegister)]) {
        
        [self.delegate didClickRegister];
    }
}

- (void)setLoginInfo:(NSString *)imageName andTitle:(NSString *)title{
    
    if (imageName != nil && title != nil) {
        
        self.iconImageView.image = [UIImage imageNamed:imageName];
        
        self.midlabel.text = title;
        
    }else{
        
        self.circleImageView.hidden = NO;
        
        [self startAnim];
    }
    
}

- (void)startAnim{
    
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    
    basicAnimation.fromValue = [NSNumber numberWithFloat:0];
    
    basicAnimation.toValue = [NSNumber numberWithFloat:2.0 * M_PI];
    
    basicAnimation.repeatCount = MAXFLOAT;
    
    basicAnimation.duration = 20;
    
    basicAnimation.removedOnCompletion = NO;
    
    [self.circleImageView.layer addAnimation:basicAnimation forKey:nil];
    
}

- (UIButton *)registerbutton{
    
    if (!_registerbutton) {
        
        _registerbutton = [[UIButton alloc] init];
        
        [_registerbutton  setBackgroundImage:[UIImage imageNamed:@"common_button_white"] forState:UIControlStateNormal];
        
        [_registerbutton setTitle:@"注册" forState:UIControlStateNormal];
        
        [_registerbutton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        
        [_registerbutton addTarget:self action:@selector(didClickRegister) forControlEvents:UIControlEventTouchUpInside];
        
        _registerbutton.titleLabel.font = [UIFont systemFontOfSize:15];
    
    }
    
    return _registerbutton;
}

- (UILabel *)midlabel{
    
    if (!_midlabel) {
        
        _midlabel = [[UILabel alloc] init];
        
        _midlabel.textColor = [UIColor blackColor];
        
        _midlabel.numberOfLines = 0;
        
        _midlabel.preferredMaxLayoutWidth = 280;
        
        _midlabel.textAlignment = NSTextAlignmentCenter;
        
        _midlabel.text = @"关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜";
    
    }
    
    return _midlabel;
}

- (UIImageView *)circleImageView{
    
    if (!_circleImageView) {
        
        _circleImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_smallicon"]];
        
        _circleImageView.hidden = YES;
        
        [_circleImageView sizeToFit];
    }
    
    return _circleImageView;
}
- (UIImageView *)iconImageView{
    
    if (!_iconImageView) {
        
        _iconImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_house"]];

        
        [_iconImageView sizeToFit];
    }
    
    return _iconImageView;
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    
    [self addSubview:self.circleImageView];
    
    [self addSubview:self.coverImageView];
    
    [self addSubview:self.iconImageView];
    
    [self addSubview:self.midlabel];
    
    [self addSubview:self.registerbutton];
    
    [self addSubview:self.loginButton];
    
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    [self.iconImageView makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self.centerX);
        
        make.centerY.equalTo(self.centerY);
        
    }];
    
    [self.circleImageView makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.centerX);
        
        make.centerY.equalTo(self.centerY);
    }];

    [self.midlabel makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.centerX);
        
        make.top.equalTo(self.circleImageView.bottom).offset(15);
        
    }];
    
    [self.registerbutton makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(self.midlabel.leading);
        
        make.top.equalTo(self.midlabel.bottom).offset(15);
        
        make.width.equalTo(100);
    }];
    
    [self.loginButton makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.midlabel.right);
        
        make.top.equalTo(self.midlabel.bottom).offset(15);
        
        make.width.equalTo(100);
    }];
    
    [self.coverImageView makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self.circleImageView.centerX);
        
        make.bottom.equalTo(self.circleImageView.bottom);
        
    }];
    
    
}

@end
